package com.bjbn.controller;

import com.bjbn.entity.BridgePowerInfoHistory;
import com.bjbn.service.BridgePowerInfoHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 廊桥电源信息历史控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/bridge-power-info-history")
@Tag(name = "廊桥电源信息历史管理", description = "提供廊桥电源信息历史的查询和搜索API接口")
public class BridgePowerInfoHistoryController {

    @Autowired
    private BridgePowerInfoHistoryService bridgePowerInfoHistoryService;

    /**
     * 根据ID查询廊桥电源信息历史
     */
    @GetMapping("/{id}")
    @Operation(
            summary = "根据ID查询廊桥电源信息历史",
            description = "根据指定的ID查询廊桥电源信息历史详情"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "查询成功",
                    content = @Content(schema = @Schema(implementation = BridgePowerInfoHistory.class))
            ),
            @ApiResponse(
                    responseCode = "404",
                    description = "未找到指定ID的廊桥电源信息历史"
            )
    })
    public BridgePowerInfoHistory findById(@Parameter(description = "廊桥电源信息历史ID") @PathVariable String id) {
        log.info("根据ID查询廊桥电源信息历史，ID: {}", id);
        return bridgePowerInfoHistoryService.findById(id);
    }

    /**
     * 根据设备名称查询廊桥电源信息历史
     */
    @GetMapping("/search/device-name")
    @Operation(
            summary = "根据设备名称查询廊桥电源信息历史",
            description = "根据设备名称模糊查询廊桥电源信息历史"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "查询成功",
                    content = @Content(schema = @Schema(implementation = List.class))
            )
    })
    public List<BridgePowerInfoHistory> findByDeviceNameContaining(
            @Parameter(description = "设备名称") @RequestParam String deviceName) {
        log.info("根据设备名称查询廊桥电源信息历史，设备名称: {}", deviceName);
        return bridgePowerInfoHistoryService.findByDeviceNameContaining(deviceName);
    }

    /**
     * 根据厂家查询廊桥电源信息历史
     */
    @GetMapping("/search/brand")
    @Operation(
            summary = "根据厂家查询廊桥电源信息历史",
            description = "根据厂家查询廊桥电源信息历史"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "查询成功",
                    content = @Content(schema = @Schema(implementation = List.class))
            )
    })
    public List<BridgePowerInfoHistory> findByBrand(
            @Parameter(description = "厂家") @RequestParam String brand) {
        log.info("根据厂家查询廊桥电源信息历史，厂家: {}", brand);
        return bridgePowerInfoHistoryService.findByBrand(brand);
    }

    /**
     * 根据区域查询廊桥电源信息历史
     */
    @GetMapping("/search/area")
    @Operation(
            summary = "根据区域查询廊桥电源信息历史",
            description = "根据区域查询廊桥电源信息历史"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "查询成功",
                    content = @Content(schema = @Schema(implementation = List.class))
            )
    })
    public List<BridgePowerInfoHistory> findByArea(
            @Parameter(description = "区域") @RequestParam String area) {
        log.info("根据区域查询廊桥电源信息历史，区域: {}", area);
        return bridgePowerInfoHistoryService.findByArea(area);
    }

    /**
     * 根据设备组查询廊桥电源信息历史
     */
    @GetMapping("/search/device-group")
    @Operation(
            summary = "根据设备组查询廊桥电源信息历史",
            description = "根据设备组查询廊桥电源信息历史"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "查询成功",
                    content = @Content(schema = @Schema(implementation = List.class))
            )
    })
    public List<BridgePowerInfoHistory> findByDeviceGroup(
            @Parameter(description = "设备组") @RequestParam String deviceGroup) {
        log.info("根据设备组查询廊桥电源信息历史，设备组: {}", deviceGroup);
        return bridgePowerInfoHistoryService.findByDeviceGroup(deviceGroup);
    }

    /**
     * 根据设备状态查询廊桥电源信息历史
     */
    @GetMapping("/search/sta")
    @Operation(
            summary = "根据设备状态查询廊桥电源信息历史",
            description = "根据设备状态查询廊桥电源信息历史"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "查询成功",
                    content = @Content(schema = @Schema(implementation = List.class))
            )
    })
    public List<BridgePowerInfoHistory> findBySta(
            @Parameter(description = "设备状态") @RequestParam String sta) {
        log.info("根据设备状态查询廊桥电源信息历史，设备状态: {}", sta);
        return bridgePowerInfoHistoryService.findBySta(sta);
    }

    /**
     * 根据IP地址查询廊桥电源信息历史
     */
    @GetMapping("/search/ip")
    @Operation(
            summary = "根据IP地址查询廊桥电源信息历史",
            description = "根据IP地址查询廊桥电源信息历史"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "查询成功",
                    content = @Content(schema = @Schema(implementation = List.class))
            )
    })
    public List<BridgePowerInfoHistory> findByIp(
            @Parameter(description = "IP地址") @RequestParam String ip) {
        log.info("根据IP地址查询廊桥电源信息历史，IP地址: {}", ip);
        return bridgePowerInfoHistoryService.findByIp(ip);
    }

    /**
     * 根据责任部门查询廊桥电源信息历史
     */
    @GetMapping("/search/dept")
    @Operation(
            summary = "根据责任部门查询廊桥电源信息历史",
            description = "根据责任部门查询廊桥电源信息历史"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "查询成功",
                    content = @Content(schema = @Schema(implementation = List.class))
            )
    })
    public List<BridgePowerInfoHistory> findByDept(
            @Parameter(description = "责任部门") @RequestParam String dept) {
        log.info("根据责任部门查询廊桥电源信息历史，责任部门: {}", dept);
        return bridgePowerInfoHistoryService.findByDept(dept);
    }

    /**
     * 根据时间范围查询廊桥电源信息历史
     */
    @GetMapping("/search/time-range")
    @Operation(
            summary = "根据时间范围查询廊桥电源信息历史",
            description = "根据时间范围查询廊桥电源信息历史"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "查询成功",
                    content = @Content(schema = @Schema(implementation = List.class))
            )
    })
    public List<BridgePowerInfoHistory> findByTimeRange(
            @Parameter(description = "开始时间") @RequestParam String startTime,
            @Parameter(description = "结束时间") @RequestParam String endTime) {
        log.info("根据时间范围查询廊桥电源信息历史，开始时间: {}，结束时间: {}", startTime, endTime);
        LocalDateTime start = LocalDateTime.parse(startTime);
        LocalDateTime end = LocalDateTime.parse(endTime);
        return bridgePowerInfoHistoryService.findByTimeRange(start, end);
    }

    /**
     * 统计廊桥电源信息历史总数
     */
    @GetMapping("/count")
    @Operation(
            summary = "统计廊桥电源信息历史总数",
            description = "统计ES中廊桥电源信息历史的总数量"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "统计成功",
                    content = @Content(schema = @Schema(implementation = Long.class))
            )
    })
    public long count() {
        log.info("统计廊桥电源信息历史总数");
        return bridgePowerInfoHistoryService.count();
    }

    /**
     * 清空廊桥电源信息历史索引
     */
    @DeleteMapping("/clear")
    @Operation(
            summary = "清空廊桥电源信息历史索引",
            description = "清空ES中所有廊桥电源信息历史数据"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "清空成功",
                    content = @Content(schema = @Schema(implementation = Boolean.class))
            )
    })
    public boolean clearAll() {
        log.info("清空廊桥电源信息历史索引");
        return bridgePowerInfoHistoryService.deleteAll();
    }
}
